package com.lzx.applib.anim;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.LayoutAnimationController;

/**
 * @author Lzx
 *
 * 2016-11-03 16:33
 * 
 * @usage 可按照以下代码使用：GridView的动画切换
 * <pre>
 * Animation amin = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
 * CustomLayoutAnimationController controller = new CustomLayoutAnimationController(amin);
 * controller.setDelay(0.3f);
 * controller.setOrder(CustomLayoutAnimationController.ORDER_CUSTOM);
 * controller.setIndexOrderListener(new Callback() {
 * 	
 * 	public int onIndex(int coute, int index) {
 * 		int numColumn = gridView.getNumColumns();
 * 		return index/numColumn + index%numColumn;
 * 	}
 * });
 * gridView.setLayoutAnimation(controller);
 * </pre>
 * 
 */
public class CustomLayoutAnimationController extends LayoutAnimationController{

	public static final int ORDER_CUSTOM = 3;
	
	private Callback callback;

	public CustomLayoutAnimationController(Animation animation) {
		super(animation);
	}

	public CustomLayoutAnimationController(Animation animation, float delay) {
		super(animation, delay);
	}

	public CustomLayoutAnimationController(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setIndexOrderListener(Callback callback) {
		this.callback = callback;
	}
	
	@Override
	protected int getTransformedIndex(AnimationParameters params) {
		if (callback != null && getOrder() == ORDER_CUSTOM) {
			return callback.onIndex(params.count, params.index);
		}
		return super.getTransformedIndex(params);
	}

	public interface Callback {
		public int onIndex(int coute, int index);
	}
}
